﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;

namespace SCM.Manage.Controllers
{
    [Authorize]
    public class BasicSupplierController : BaseController
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult SelectView()
        {
            return View();
        }

        #region 添加
        public ActionResult AddView()
        {
            return View();
        }

        public IActionResult Save(T_Supplier entity)
        {
            #region 数据验证
            if (string.IsNullOrWhiteSpace(entity.SupplierName))
            {
                return Error("名称不能为空");
            }

            if (string.IsNullOrWhiteSpace(entity.ShortName))
            {
                return Error("短名不能为空");
            }

            if (string.IsNullOrWhiteSpace(entity.IdentityNumber))
            {
                return Error("纳税人识别号不能为空");
            }

            #endregion

            if (string.IsNullOrWhiteSpace(entity.ID))
            {
                entity.ID = Guid.NewGuid().ToString();
                entity.Creator = CurrentUserId;
                entity.CreatorName = CurrentUserName;
                entity.CompanyID = CurrentCompanyID;

                string id = GetService<ISupplierService>().Add(entity);
                return !string.IsNullOrWhiteSpace(id) ? GetOk(id) : Error("保存失败");
            }
            else
            {
                entity.ModifyUserId = CurrentUserId;
                entity.ModifyUserName = CurrentUserName;

                bool result = GetService<ISupplierService>().Update(entity);
                return result ? GetOk(entity.ID) : Error("保存失败");
            }
        }
        #endregion

        #region 查询
        [HttpPost]
        public IActionResult Search(string suplierName, string shortName, string identityNumber, int index = 1)
        {
            var service = GetService<ISupplierService>();
            var dataPage = service.Query(CurrentCompanyID, suplierName, shortName, identityNumber, new Common.PageInfo() { PageIndex = index, PageSize = 10 });
            return GetOk(new { totalCount = dataPage.Page.DataCount, items = dataPage.Data });
        }

        #endregion

        #region 编辑
        public IActionResult EditView(string id)
        {
            ViewBag.ContactList = GetService<ISupplierContactService>().GetList(id);
            var model = GetService<ISupplierService>().GetById(id);
            return View(model);
        }

        public IActionResult Modify(T_Supplier entity)
        {
            entity.ModifyUserId = CurrentUserId;
            entity.ModifyUserName = CurrentUserName;

            bool result = GetService<ISupplierService>().Update(entity);
            return result ? GetOk(result) : Error("保存失败");
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除  【支持批量操作】
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult Delete(string ids)
        {
            if (string.IsNullOrWhiteSpace(ids) || ids.Split(',').Length < 0)
            {
                return Error("请选择数据");
            }
            var delIds = ids.Split(',').ToList();
            bool result = GetService<ISupplierService>().Delete(delIds);
            return result ? GetOk(result) : Error("删除失败");
        }
        #endregion

        #region 联系人列表
        public IActionResult SaveContacterList(string supplierId, List<T_Supplier_Contact> contactList)
        {
            var service = GetService<ISupplierContactService>();
            if (contactList == null || contactList.Count == 0)
            {
                service.Delete(supplierId);
                return GetOk("");
            }

            contactList.ForEach(item => { if (string.IsNullOrWhiteSpace(item.ID)) { item.ID = Guid.NewGuid().ToString(); } });
            bool result = service.Save(contactList);
            return result ? GetOk(string.Join(',', contactList.Select(p => p.ID))) : Error("保存失败");
        }
        #endregion
    }
}
